/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.A简单题.两数之和IV输入BST;

import java.util.HashSet;
import java.util.LinkedList;

public class Main {

    public boolean findTarget(TreeNode root, int k) {
        LinkedList<TreeNode> middle1 = new LinkedList<>(), middle2 = new LinkedList<>();
        HashSet<Integer> data = new HashSet<>();
        middle1.addLast(root);
        while (!middle1.isEmpty()) {
            while (!middle1.isEmpty()) {
                if (data.contains(middle1.peekFirst().val))
                    return true;
                TreeNode treeNode = middle1.pollFirst();
                data.add(k - treeNode.val);
                if (treeNode.left != null) {
                    middle2.addLast(treeNode.left);
                }
                if (treeNode.right != null) {
                    middle2.addLast(treeNode.right);
                }
            }
            middle1.addAll(middle2);
            middle2.clear();

        }
        return false;
    }


    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
